/*
 * Initializes the decision lines proxy
 * 
 * Author: John Manero
 */
require('./js2xml');

var Debug = require('./debug');
debug = new Debug({
	mode : 'net',
	level : 'debug',
	port : 9002
});

var express = require('express');
var util = require('util');

var ccSession = require('./control/SessionControl');
var common = require('./common');
var DefaultRedir = require('./defaultRedir');

var app = express.createServer();
var sessions = {}; // Hash Table of sessions by session ID

// ---- Reap sessions ----
setInterval(function() {
	var exp = Date.now() - common.session.maxLen;
	
	for(var s in sessions) {
		if(sessions[s].lastAccess < exp || sessions[s].destroyFlag) {
			sessions[s].destroy();
			delete sessions[s];
		}
	}
}, common.session.reapInterval);
// -----------------------

// ---- Conigure Express Server ----
app.configure(function() {
	app.use(express.methodOverride());
	app.use(express.bodyParser());
	app.use(express.cookieParser());
	app.use(function(req, res, next) {
		debug.log("Incoming HTTP Request");
		debug.log("	Request Path: " + req.path);
		next();
	});
	app.use(ccSession(sessions));
	app.use(app.router);
	app.use(function(req, res, next) {
		req.session.releaseClient();
		debug.debug("	Fall-through to file");
		next();
	});
	app.use(express.static(__dirname + '/../www'));
	app.use(DefaultRedir());
});
//----------------------------------

// Import Controls
require('./control/KeepaliveControl').create(app);
require('./control/CommandControl').create(app);

app.listen(9001);